package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.ism.constant.SignatureConstant;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmTaskService;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.exception.BusinessMessage;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class IsmGetOrdersComponentServiceImpl implements IsmMethodService {
    @Resource
    IsmTaskService ismTaskService;
    @Resource
    IsmCommonService ismCommonService;

    public IsmGetOrdersComponentServiceImpl(){}

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) {
        JSONObject result = new JSONObject();
        String orders = this.checkAndGetData(businessParams);
        JSONObject resData = ismCommonService.getOrdersComponent(orders, null);
        ismTaskService.deleteTaskByParams(sysCode, serviceCode, serviceCode, null,
                businessParams.getString(SignatureConstant.INPUT_PARAMS));
        result.put("flag", "success");
        result.put("data", resData);
        return result;
    }
    //入参检查
    private String checkAndGetData(JSONObject busParams) throws BusinessMessage{
        String orders = busParams.getJSONObject(SignatureConstant.INPUT_PARAMS).getString("orders");
        if(StringUtil.isEmpty(orders)) {
            throw new BusinessMessage("请求中缺少orders参数，请检查！");
        }
        return orders;
    }

    @Override
    public String methodName(){
        return "getOrdersComponent";
    }
}
